package com.api.gbhg;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;

public class APP {
    /**
     * 查询渠道实际报销金额
     * @param yslylx 预算来源（当年 or 上年）
     * @param srlx  收入类型（财政 or 非财政）
     * @param srfl 非中央收入分类
     * @param dfczbkxm 地方财政拨款项目id
     * @param zyczbksr  中央财政核批id
     * @return
     */
    public Double getSjbxje(RecordSet rs, String yslylx, String srlx, String srfl, String dfczbkxm, String zyczbksr){
        String baseSql = "SELECT ISNULL(SUM(d1.tybxje-isnull(d1.cxje,0)),0) AS sjbxje FROM dbo.uf_bxjl_kj_dt1 d1 \n" +
                "LEFT JOIN uf_bxjl_kj d ON d1.lcid=d.lcid\n" +
                "LEFT JOIN dbo.workflow_requestbase wr ON d1.lcid=wr.requestid\n" +
                "LEFT JOIN workflow_base wb ON wr.workflowid=wb.id\n" +
                "WHERE  wr.currentnodetype=3 and ysly="+(yslylx.startsWith("当年")?"0":"1")+" and zcqd="+srlx+" \n";
        if(Integer.parseInt(srlx)==0){
            //财政拨款
            String kmj = new BaseBean().getPropValue("Verification","yj");
            if(zyczbksr.split("_")[0].equals(kmj)){
                baseSql += " and kmjxj=0 and dygnkmbid="+zyczbksr.split("_")[1];
            }else{
                baseSql += " and kmjxj=1 and dygnkmbid="+zyczbksr.split("_")[1];
            }
        }else{
            if(srfl.equals("5") || srfl.equals("8") ){
                baseSql += " and fzysrfl="+srfl+" and zcdfczbkxm="+dfczbkxm;
            }else {
                baseSql += " and fzysrfl=" + srfl ;
            }
        }
        System.err.println(baseSql);
        rs.execute(baseSql);
        if(rs.next()){
            return rs.getDouble("sjbxje");
        }
        return 0.00;
    }

    /**
     * 查询項目报销金额
     * @param lcid 项目流程id
     * @return
     */
    public Double getSjbxje_Project(RecordSet rs, String lcid){
        String baseSql = "SELECT ISNULL(SUM(d1.tybxje-isnull(d1.cxje,0)),0) AS sjbxje FROM dbo.uf_bxjl_kj_dt1 d1 \n" +
                "LEFT JOIN uf_bxjl_kj d ON d1.lcid=d.lcid\n" +
                "LEFT JOIN dbo.workflow_requestbase wr ON d1.lcid=wr.requestid\n" +
                "LEFT JOIN workflow_base wb ON wr.workflowid=wb.id\n" +
                "WHERE  wr.currentnodetype=3 and ysxm="+lcid;
        rs.execute(baseSql);
        if(rs.next()){
            return rs.getDouble("sjbxje");
        }
        return 0.00;
    }

    /**
     * 查询項目冻结金额
     * @param lcid 项目流程id
     * @return
     */
    public Double getDjje_Project(RecordSet rs, String lcid){
        String baseSql = "SELECT ISNULL(SUM(sqbxje-isnull(cxje,0)),0)  as dj FROM (\n" +
                " SELECT wr.requestid,jl.lclx,jl.sqbxje FROM dbo.uf_bxsjjl  jl LEFT JOIN workflow_requestbase wr ON jl.lcid=wr.requestid\n" +
                " WHERE ysxm="+lcid+" AND wr.currentnodetype in(1,2) )t WHERE t.requestid IS NOT NULL";
        rs.execute(baseSql);
        if(rs.next()){
            return rs.getDouble("dj");
        }
        return 0.00;
    }
}
